package com.free.show;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

/**
 * <p>
 * Title: Eclipse Plugin Development
 * </p>
 * <p>
 * Description: Free download
 * </p>
 * <p>
 * mail: ganshm@gmail.com
 * </p>
 */

public class CoolBarExample extends ApplicationWindow
{
    public CoolBarExample()
    {
        super(null);
    }
    
    protected Control createContents(Composite parent)
    {
        // --- Create the window title. ---
        
        getShell().setText("CoolBar Test");
        
        String asCoolItemSection[] = {"File", "Formatting", "Search"};
        // 添加CoolBar
        CoolBar composite = new CoolBar(parent, SWT.NONE);
        for (int idxCoolItem = 0; idxCoolItem < 3; ++idxCoolItem)
        {
            CoolItem item = new CoolItem(composite, SWT.NONE);
            // 添加子组件
            ToolBar tb = new ToolBar(composite, SWT.FLAT);
            for (int idxItem = 0; idxItem < 3; ++idxItem)
            {
                ToolItem ti = new ToolItem(tb, SWT.NONE);
                ti.setText(asCoolItemSection[idxCoolItem] + " Item #" + idxItem);
            }
            Point p = tb.computeSize(SWT.DEFAULT, SWT.DEFAULT);
            tb.setSize(p);
            Point p2 = item.computeSize(p.x, p.y);
            
            // 设置为一个CoolItem的控制类
            item.setControl(tb);
            item.setSize(p2);
        }
        return composite;
    }
    
    public static void main(String[] args)
    {
        // --- Display SWTCoolBarTestDemo until the window is closed. ---
        
        CoolBarExample app = new CoolBarExample();
        app.setBlockOnOpen(true);
        app.open();
        Display.getCurrent().dispose();
    }
}
